package cn.itcast.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CommonConfig{

    public static final String DIRECT_EXCHANGE = "direct.exchange";
    public static final String DIRECT_ROUTE_KEY = "direct.key";
    public static final String DIRECT_QUEUE = "direct.queue";


    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange(DIRECT_EXCHANGE);
    }

    @Bean
    public Queue directQueue(){
        return new Queue(DIRECT_QUEUE);
    }

    @Bean
    public Binding bindDirectQueue(){
        return BindingBuilder.bind(directQueue()).to(directExchange()).with(DIRECT_ROUTE_KEY);
    }

    /**
     * @return
     * 交换机持久化的三种代码编写方式
     */
    @Bean
    public DirectExchange directExchange1(){
        return ExchangeBuilder.directExchange(DIRECT_EXCHANGE).durable(true).build();
    }
    /*
    *
    * 队列持久化的三种代码编写方式
    * */
    @Bean
    public Queue directQueue1(){
        return QueueBuilder.durable(DIRECT_QUEUE).build();
    }
    // 声明普通的 simple.queue队列，并且为其指定死信交换机：dl.direct
    @Bean
    public  Queue SimpleQueue2(){
        return QueueBuilder.durable("simple.queue")// 声明队列
                .deadLetterExchange("dl.direct")// 指定死信交换机
                .build();// 返回队列
    }
    // 声明死信交换机 dl.direct
    public DirectExchange dlExchange(){
        return  new DirectExchange("dl.direct",true,false);
    }
    // 声明存储死信的队列 dl.queue
    public Queue dlQueue(){
        return new Queue("dl.queue",true);// true表示队列持久化
    }
    // 将死信队列 与 死信交换机绑定
    public Binding bindDlQueue(){
      return BindingBuilder.bind(dlQueue()).to(dlExchange()).with("simple");// 绑定的key 为 simple
    }
}
